package cn.handong.tool.Java文件操作;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class 批量修改文件名称 {

    /**
     * 名字改为我们的
     * @throws FileNotFoundException
     */
    @Test
    public void batchChangeFileName() throws FileNotFoundException {
        String path = "C:\\hd\\hdhd";//文件位置
        String suffix = ".txt";      //后缀
        List<String> fileNameList = new ArrayList<>();
        try (Stream<Path> paths = Files.walk(Paths.get(path))){
            List<Path> fileNames = paths.filter(Files::isRegularFile).collect(Collectors.toList());
            int i = 1;
            for (Path fileName : fileNames) {
                String myPath = fileName.toFile().getPath();
                fileNameList.add(myPath);
                reName(myPath,path + "\\" + i++ + suffix);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String fileNamesPath = "C:\\Users\\sqkb8v\\IdeaProjects\\my-general-tool";
        String fileNamesName = "fileNames.txt";
        writeToTxt(fileNamesPath,fileNamesName,fileNameList);
    }

    /**
     * 再把名字该回去
     * @throws IOException
     */
    @Test
    public void batchReturnFileName() throws IOException {
        //绝对路径
        String fileNamesPath = "C:\\Users\\sqkb8v\\IdeaProjects\\my-general-tool\\fileNames.txt";
        List<String> nameList = getFileNames(fileNamesPath);
        String filePath = "C:\\hd\\hdhd";//文件位置
        try (Stream<Path> paths = Files.walk(Paths.get(filePath))){
            List<Path> fileNames = paths.filter(Files::isRegularFile).collect(Collectors.toList());
            int i = 0;
            for (Path fileName : fileNames) {
                String myPath = fileName.toFile().getPath();
                reName(myPath,nameList.get(i++));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    
    public List<String> getFileNames(String fileNamesPath) throws IOException {
//        String path = "list.txt"; //使用相对路径的话要放在和src同基本的文件夹
        BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(fileNamesPath)), StandardCharsets.UTF_8));
        String lineTxt;
        List<String> nameList = new ArrayList<>();
        // 逐行读取
        while ((lineTxt = br.readLine()) != null) {
            System.out.println(lineTxt);
            if (StringUtils.isNotBlank(lineTxt)){
                nameList.add(lineTxt);
            }
        }
        br.close();
        return nameList;
    }

    /**
     * 文件重命名
     */
    public void reName(String oldPath, String newPath){
        File file = new File(oldPath);
        File newFile = new File(newPath);
        file.renameTo(newFile);
    }

    public void writeToTxt(String folder, String file, List<String> messageList) throws FileNotFoundException {
        File files = new File(folder);
        files.mkdirs();

        FileOutputStream fos = new FileOutputStream(folder + File.separator + file);

        // 逐行写入
        PrintWriter pw = new PrintWriter(fos);
        for (String message : messageList) {
            pw.println(message);
        }
        pw.close();
    }
}
